export const analysis = (cellData,dataSource) =>{
    if(cellData.cellTag.tag == 'base'){
        return '${'+dataSource+'.'+cellData.field+'}'
    }
    else if(cellData.cellTag.tag == 'dict'){   
        let str = ''    
       for (var key in cellData.cellTag){
          let str1=cellData.cellTag[key]?key+'="'+cellData.cellTag[key]+'"':''
          str = str+' '+str1
       }
       return '<dict'+' '+str+'>'+'${'+dataSource+'.'+cellData.field+'}'+'</dict>'
    }
    else if(cellData.cellTag.tag == 'img'){
        return '<img>'+'${'+dataSource+'.'+cellData.field+'}'+'</img>'
    }
    else if(cellData.cellTag.tag == 'fcol'){
        let str = ''    
        let end = ''//结束标签
        for (var key in cellData.cellTag){
            if(key == 'end'){
                end = cellData.cellTag[key] == true?'</fcol>':''
            }
            else{
                let str1=cellData.cellTag[key]?key+'="'+cellData.cellTag[key]+'"':''
                str = str+' '+str1
            }
        }
        return  '<fcol'+' '+str+'>'+'${'+dataSource+'.'+cellData.field+'}'+end
    }
    else if(cellData.cellTag.tag == 'frow'){
        let str = ''    
        let end = ''//结束标签
        for (var key in cellData.cellTag){
            if(key === 'end'){
                end = cellData.cellTag[key] == true?'</frow>':''
            }
            else{
                let str1=cellData.cellTag[key]?key+'="'+cellData.cellTag[key]+'"':''
                str = str+' '+str1
            }
        }
        return  '<fcol'+' '+str+'>'+'${'+dataSource+'.'+cellData.field+'}'+end
    }
}
export const  tranferEl = (settingList,row,column,cellTag) =>{
    if(cellTag.tag == 'fcol' || cellTag.tag == 'frow'){
        return cellTag
    }
    return findSmallestContainingRegion(row,column,settingList)
}
function findSmallestContainingRegion(pointRow, pointCol, regions) {  
    let smallestRegion =null;  
    let smallestArea = Infinity;  
    
    // 计算区域面积的函数  
    function calculateArea(region) {  
      const rowRange = region.row[1] - region.row[0] + 1;  
      const colRange = region.col[1] - region.col[0] + 1;  
      return rowRange * colRange;  
    }  
    
    // 遍历所有区域，检查点是否在每个区域内  
    regions.forEach(region => {  
      const regionRowMin = region.row[0];  
      const regionRowMax = region.row[1];  
      const regionColMin = region.col[0];  
      const regionColMax = region.col[1];  
    
      // 如果点在区域内  
      if (  
        pointRow >= regionRowMin &&  
        pointRow <= regionRowMax &&  
        pointCol >= regionColMin &&  
        pointCol <= regionColMax  
      ) {  
        // 计算区域面积  
        const area = calculateArea(region);  
    
        // 如果这是第一个包含该点的区域，或者它的面积更小  
        if (smallestArea === Infinity || area < smallestArea) {  
          smallestArea = area;  
          smallestRegion = region;  
          
        }  
      }  
    });  
    
    return smallestRegion; // 返回面积最小的包含该点的区域，如果没有则返回null  
  } 
export const extractNodesAndFields=(tree)=> {  
    const nodesArray = []; // 存储所有的节点（包括children）  
    const fieldsArray = []; // 存储所有的fields  
    
    function traverse(node) { 
      node.uid = node.parentId+node.id 
      // 将当前节点添加到nodesArray中  
      nodesArray.push({ ...node});  
    
      // 如果节点有fields，将它们添加到fieldsArray中  
      if (node.fields) {
        node.fields.forEach(ele=>{
          ele.uid = ele.parentId+ele.id
        }) 
        fieldsArray.push(...node.fields);  
      }  
    
      // 如果节点有children，递归遍历它们  
      if (node.children) {  
        node.children.forEach(child => {  
            traverse(child); // 递归调用，增加深度  
          });  
      }  
    }  
    
    // 遍历树的每一个顶级节点  
    tree.forEach(node => traverse(node));  
    
    return {  
      nodesArray,  
      fieldsArray  
    };  
  }  
